package com.econ.powercloud.ems.service.dao;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.econ.powercloud.ems.dal.entity.ExtIotDeviceCalculation;
import com.econ.powercloud.ems.dal.mapper.ExtIotDeviceCalculationMapper;
import com.mysql.cj.util.StringUtils;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @Author : zjf
 * @Description :
 * @Date : 2022/11/14 15:13
 * @Modified by : zjf
 */
@Repository
public class ExtIotDeviceCalculationDAO extends ServiceImpl<ExtIotDeviceCalculationMapper, ExtIotDeviceCalculation> {

    /**
     * @param measureType: 计算点号类型
     * @param search : 点号名称或标识
     * @return
     */
    public Page<ExtIotDeviceCalculation> selectPageBySearch(Integer devicePid, Integer measureType, String search, Integer pageNo, Integer pageSize){
        LambdaQueryWrapper<ExtIotDeviceCalculation> lambdaQuery = Wrappers.lambdaQuery();
        lambdaQuery.eq(ExtIotDeviceCalculation::getComputeFlag,1);
        lambdaQuery.eq(ExtIotDeviceCalculation::getDevicePid,devicePid);
        lambdaQuery.eq(ExtIotDeviceCalculation::getMeasureType,measureType);
        lambdaQuery.and(!StringUtils.isNullOrEmpty(search),wrapper -> wrapper.like(ExtIotDeviceCalculation::getComputeCode, search).or().like(ExtIotDeviceCalculation::getComputeName, search));
        Page<ExtIotDeviceCalculation> page = new Page<>(pageNo, pageSize);
        return this.baseMapper.selectPage(page,lambdaQuery);
    }

    public ExtIotDeviceCalculation selectByComputeCode(String computeCode) {
        LambdaQueryWrapper<ExtIotDeviceCalculation> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(ExtIotDeviceCalculation::getComputeCode,computeCode);
        wrapper.last("limit 1");
        return this.baseMapper.selectOne(wrapper);
    }

    public List<ExtIotDeviceCalculation> selectListByComputeCodes(List<String> computeCodes){
        LambdaQueryWrapper<ExtIotDeviceCalculation> wrapper = Wrappers.lambdaQuery();
        wrapper.in(ExtIotDeviceCalculation::getComputeCode,computeCodes);
        return this.baseMapper.selectList(wrapper);
    }

    public List<ExtIotDeviceCalculation> selectByDevicePidAndMeasureType(Integer devicePid,Integer measureType){
        LambdaQueryWrapper<ExtIotDeviceCalculation> lambdaQuery = Wrappers.lambdaQuery();
        lambdaQuery.eq(ExtIotDeviceCalculation::getDevicePid,devicePid);
        lambdaQuery.eq(ExtIotDeviceCalculation::getMeasureType,measureType);
        return this.baseMapper.selectList(lambdaQuery);
    }

}
